package com.wedistudio.android.framework.fetcher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.turbomanage.httpclient.HttpResponse;
import com.wedistudio.android.framework.service.ServiceException;
import com.wedistudio.android.framework.service.ServiceFetcher;

public class DefaultFetcher implements ServiceFetcher {

	@Override
	public Map<URLParams, String> fetch(List<URLParams> urls)
			throws ServiceException {
		Map<URLParams, String> result = new HashMap<URLParams, String>();
		for (URLParams urlparams : urls) {
			result.put(urlparams, convertToString(BasicFetcher.get(
					urlparams.getUrl(), urlparams.getParams())));
		}

		return result;
	}

	private String convertToString(HttpResponse httpResponse) {
		return httpResponse.getBodyAsString();
	}

	@Override
	public String fetch(URLParams url) throws ServiceException {
		return convertToString(BasicFetcher.get(url.getUrl(), url.getParams()));
	}

	static public String fetch(String url) throws ServiceException {
		return (new DefaultFetcher()).fetch(URLParams.create(URLBuilder
				.create(url)));
	}

}
